<template>
  <div class="exception-page">
    <div class="img">
      <img :src="img || config[type].img" />
    </div>
    <div class="content">
      <h1>{{title || config[type].title}}</h1>
      <div class="desc">{{desc || config[type].desc}}</div>
      <div class="action">
        <Button size="default" type="primary" @click="backHome">返回首页</Button>
        <Button size="large" type="text" @click="backHome">返回上一页({{ second }}s)</Button>
      </div>
    </div>
  </div>
</template>

<script>
import error401 from '@/assets/images/error/401.svg';
import error404 from '@/assets/images/error/404.svg';
import error500 from '@/assets/images/error/500.svg';

export default {
  name: 'Exception',
  props: {
    'type':{
      type: String,
      default: '404'
    },
    'img':{type: String},
    'title':{type: String},
    'desc':{type: String},
  },
  data () {
    return {
      second: 5,
      config: {
        401: {
          img: error401,
          title: '401',
          desc: '抱歉，你无权访问该页面'
          },
          404: {
            img: error404,
            title: '404',
            desc: '抱歉，你访问的页面不存在或仍在开发中'
          },
          500: {
            img: error500,
            title: '500',
            desc: '抱歉，服务器出错了'
          }
      }
    }
  },
  mounted () {
    this.timer = setInterval(() => {
      if (this.second === 0){
        this.backPrev();
      } else {
        this.second--
      };
    }, 1000);
  },
  beforeDestroy () {
    clearInterval(this.timer);
  },
  methods: {
    // 回到首页
    backHome() {
     this.$router.replace({
        name: this.$config.homeName
      });
    },
    // 返回上一页
    backPrev () {
      this.$router.go(-1);
    }
  }
}
</script>
<style lang="less" scoped>
  .exception-page{
    border-radius: 4px;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    .img{
      padding-right: 52px;
      zoom: 1;
      img{
        max-width: 430px;
      }
    }
    .content{
      h1{
        color: #434e59;
        font-size: 72px;
        font-weight: 600;
        line-height: 72px;
        margin-bottom: 24px;
      }
      .desc{
        color: rgba(0, 0, 0, 0.45);
        font-size: 20px;
        line-height: 28px;
        margin-bottom: 16px;
      }
    }
  }

</style>
